import { controller, httpGet, httpPost } from "inversify-express-utils";
import { inject } from "inversify";
import { UserService } from "./services";
import { Request, Response } from "express";

// 在这里定义路由，controller装饰器用于定义路由前缀
// httpGet和httpPost装饰器用于定义请求方法
// 在这里定义路由处理函数，函数名可以自定义，具体的操作在service中定义
@controller("/user")
export class UserController {
  // 在构造函数中注入service
  constructor(@inject(UserService) private readonly userService: UserService) {}

  /**
   * @api {get} /user/getAllUser 查询所有用户信息
   */
  @httpGet("/getAllUser")
  async getAllUser(req: Request, res: Response) {
    // 调用service中的getAllUser方法
    let result = await this.userService.getAllUser();
    // 返回结果
    res.send(result);
  }

  /**
   *
   * @api {post} /user/addUser 新增用户信息
   */
  @httpPost("/addUser")
  async addUser(req: Request, res: Response) {
    let { name, email } = req.body;
    let result = await this.userService.addUser({
      name,
      email,
    });
    res.send(result);
  }
}
